<?php

namespace bc\model\knowledge;

use bc\ar\MActiveRecord;

class RelationModel extends MActiveRecord
{
	
	public $autoId = 'relation_id';
	
	public static function tableName()
	{
		return '{{%knowledge_relation}}';
	}
       
        
        public function tractionInsert($primaryId,$relation_tag_id,$relation_satus)
    {
    	//$transaction = $this->getDb()->beginTransaction();
 
    	try 
    	{
	    	foreach ($relation_tag_id as $key=>$value)
	    	{       
                     
	    		$sql = 'INSERT ' . self::getTableSchema ()->name . "(relation_knowledge_id, relation_tag_id, relation_status) VALUES ( {$primaryId}, '{$value}',$relation_satus)";
	    		$this->getDb()->createCommand($sql)->execute();
                        //echo $sql;
                  
	    	}
    		
    		//$transaction->commit();
    	}
		catch (\Exception $e) 
    	{
			//$transaction->rollBack();
			throw $e;
    	}
    }
    
    public function tractionModify($id,$tagsList, $relation_status)
    {
    	//$transaction = $this->getDb()->beginTransaction();
 
    	try 
    	{
	    	foreach ($tagsList as $key=>$value)
	    	{       
                     
	    		$sql = "UPDATE bc_knowledge_relation SET relation_status = '$relation_status' WHERE relation_knowledge_id = '$id'";
                        //$sql = "update ecs_order_info set invoice_no  = '$invoice_no' ,shipping_status = 1  where order_sn  = '$order_id' ;";
	    		$this->getDb()->createCommand($sql)->execute();
                        echo $sql;
                  
	    	}
    		
    		//$transaction->commit();
    	}
		catch (\Exception $e) 
    	{
			//$transaction->rollBack();
			throw $e;
    	}
    }
    
    /*
         * 获取tag列表
         */
        public function  getTagList($params)
        {
                $this->columns = "bc_knowledge_tag.tag_id , bc_knowledge_tag.tag_name , bc_knowledge_relation.*";
                $ar = $this->find ()->select($this->columns)->leftJoin('bc_knowledge_tag', 'bc_knowledge_tag' . '.tag_id = ' . self::getTableSchema()->name . '.relation_tag_id') ->where($params);
	        //echo $ar->createCommand()->getRawSql();
		if($this->sort == null)
			$ar->orderBy ( $this->autoId . ' DESC' );
		else
			$ar->orderBy ( $this->sort );
		
		return $ar->asArray()->all();
            
        }
    
    
      
}

